<?php
namespace app\admin\controller;
use \Firebase\JWT\JWT;
use app\user\controller\User;
use think\Db;
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

/**
 * 登录页面
 * Class Logintoken
 * @package app\admin\controller
 */
class Logintoken
{

    public function yanzheng(){
      $canshu=input('canshu');
      $datas['status']=100001;
      $datas['info']=$canshu;
      return json($datas);
    }
    public function noauth(){
      $datas['status']=100002;
      $datas['info']='无访问权限';
      return json($datas);
    }
    //管理员注册功能
    public function register()
    {

      $username=input('username');
      $password=input('password');
      $repassword=input('repassword');
      $nickname=input('nickname');
      $email=input('email');
      if($password==$repassword){

        /* 调用注册接口注册用户 */
            $User   =   new User();

            $uid    =   $User->register($username, $password, $email,'','admin',$nickname='');

            if($uid>0){

                $datas['status']=1;
                $key1=HB_JWT_TOKEN;
                $key=$key1.$uid;
                if($nickname){
                   $shuju['nickname']=$nickname;
                }else{
                   $shuju['nickname']=$username;
                }
                $shuju['uid']=$uid;
                $shuju['status']=1;
                $shuju['iat']=time();
                $shuju['exp']=time()+7200;
                $shuju['nbf']=time();
                $jwt = JWT::encode($shuju, $key);
                $datas['uid']=$uid;
                $datas['access_token']=$jwt;
            }else{
              $datas['status']=0;
              $datas['info']=$uid ;
            }
      }else{
        $datas['status']=0;
        $datas['info']='密码不一致！';
      }
      return json($datas);
    }

    public function login(){
       $username=input('username');
       $password=input('password');
       $token=input('token');
       if($token==HB_TOKEN){
           $User   =   new User();
           $uid    =   $User->login($username,$password,$type=1);
           $info=Db::name('Member')->where('id',$uid)->field('id,username,nickname')->find();
           if($info){
             $datas['status']=1;
             $key1=HB_JWT_TOKEN;
             $key=$key1.$uid;
             $shuju['nickname']=$info['nickname'];
             $shuju['uid']=$uid;
               $shuju['status']=1;
             $shuju['iat']=time();
             $shuju['exp']=time()+7200;
             $shuju['nbf']=time();
             $jwt = JWT::encode($shuju, $key);
             $datas['uid']=$uid;
             $datas['access_token']=$jwt;
           }else{
             $datas['status']=0;
             $datas['info']='登录失败！账号密码不匹配';
           }
       }else{
         $datas['status']=0;
         $datas['info']='token不一致请输入正确的token';
       }
       return json($datas);
    }






}
